import re
import demjson
import requests
from pyquery import PyQuery as pq

url = 'https://mp.weixin.qq.com/profile?src=3&timestamp=1536763303&ver=1&signature=I0VzNIpVFhXGpTX8dOVn2qiRlcyq7x1lOSt7wG4iqW*5no9W6rEUpzRzk8n1lLcT3sBga*sINb5aiz2H*altMA=='

headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Cookie':'RK=1URd1JbEQU; pgv_pvi=2883516416; tvfe_boss_uuid=ffad2e51611aa32d; pgv_pvid=7173960030; ptcz=7883a9c767e8d72ebeab2d6b526f544775c077630f29bf017c60daf2a23d8b30; xid=0df827866ad7bd6734c37529b393d2b2; ua_id=1DOZlATyUQIol0G6AAAAAOVB86eADuuestEPYu2hoiI=; ue_uk=ad5a18e7ad50a59bed05aa5e1403f4fb; ue_uid=9c19f8ad56358e75cdc674da7a4e2efa; eas_sid=l1T5M3s201V6z9I7B5H4X2f2M8; ue_ts=1532172349; ue_skey=4be1e4df32fd4e5a5264e9e1a322a68a; pt2gguin=o0390982209; ptui_loginuin=390982209@qq.com; o_cookie=390982209; pac_uid=1_390982209; sig=h013e5348a3d97706c5dbcc3572bfe3ac4ac69970793754c64a1b7366410313a290fcfb0e2bcb646919',
'Host':'mp.weixin.qq.com',
'Referer':'https://mp.weixin.qq.com/profile?src=3&timestamp=1536761061&ver=1&signature=zuU1JCnDf3lOljtFORmDCEpsXIboU-0SIr6ytVaN*yEBGhqldB9gkfNaXyDm1LA*XDSwPhsSTXGonRiTHXjcdg==',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Linux; U; Android 4.4.3; zh-cn; HTC 802w Build/KTU84L) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025489 Mobile Safari/533.1 MicroMessenger/6.3.13.49_r4080b63.740 NetType/WIFI Language/zh_CN',
}

ip_resp = requests.get("http://tpv.daxiangdaili.com/ip/?tid=558902409230438&num=1").text

proxies = {
  'http': 'http://{}'.format(ip_resp.strip()),
  'https': 'http://{}'.format(ip_resp.strip()),
}
print(proxies)
# proxies = {'http': 'http://47.105.135.8:80', 'https': 'http://47.105.135.8:80'}
def get_content():

    resp = requests.get(url,headers = headers,timeout = 12, proxies = proxies,allow_redirects=False)
    print(resp.status_code)
    if resp.status_code == 200:
        doc = pq(resp.text)
        data = doc("script:last-child")
        data_str = re.findall(re.compile(r'var msgList = (.*?)};'),str(data))[0]+'}'
        data_list = demjson.decode(data_str).get("list")
        for item in data_list:
            datetime_ = item.get("comm_msg_info").get("datetime")
            num = item.get('app_msg_ext_info').get("is_multi")
            if num == 1:
                multi_tem_list = item.get('app_msg_ext_info').get("multi_app_msg_item_list")
                for info in multi_tem_list:
                    title = info.get("title")
                    content_url = 'https://mp.weixin.qq.com'+info.get("content_url").replace("amp;amp;",'')
                    print(title,content_url)
            else:
                title = item.get("app_msg_ext_info").get("title")
                print(title)



        # if resp.status_code == 302:

if __name__ == '__main__':
    get_content()